package bot;

import gui.AutoButton;
import gui.BotGui;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import datacollection.WebParser;

public class AutoEdit {
	
	private static int count = 0;
	
	private List<String> exceptions;
	public AutoEdit(){
		exceptions = new ArrayList<String>();
	}
	
	public void edit(String url, City city, String date, JTextArea code, boolean cleanup){
		Thread thread = new Thread(new AutoEditThread(url, city, date, code, cleanup));
		thread.start();
	}
	
	private int findReference(String source){
		int ret = 0;
		for (int i = 14; i < source.length(); i++){
			if (source.substring(i - 14, i).equals("==References==")){
				//find the number of new line characters
				for (int j = i; j < source.length(); j++){
					if (source.charAt(j) == '\n'){
						ret++;
					}
				}
				break;
			}
		}
		return ret;
	}
	
	
	private class AutoEditThread implements Runnable{
		
		private String url;
		private City city;
		private String date;
		private JTextArea code;
		private boolean cleanup;
		
		public AutoEditThread(String url, City city, String date, JTextArea code, boolean cleanup){
			this.url = url;
			this.city = city;
			this.date = date;
			this.code = code;
			this.cleanup = cleanup;
		}
		
		@Override
		public void run() {
			WebParser web = new WebParser();
			try{
				Robot robo = new Robot();
				String source = web.getWikiEditSource(url);
				int moveUp = findReference(source) - 1;
				//activate the edit area
				robo.mouseMove(500, 600);
				robo.mousePress(InputEvent.BUTTON1_MASK);
				robo.mouseRelease(InputEvent.BUTTON1_MASK);
				//select all
				robo.keyPress(KeyEvent.VK_CONTROL);
				robo.keyPress(KeyEvent.VK_END);
				robo.keyRelease(KeyEvent.VK_END);
				long start = System.currentTimeMillis();
				while (System.currentTimeMillis() - start < 100){}
				robo.keyPress(KeyEvent.VK_A);
				robo.keyRelease(KeyEvent.VK_A);
				robo.keyRelease(KeyEvent.VK_CONTROL);
				start = System.currentTimeMillis();
				while (System.currentTimeMillis() - start < 100){}
				//move up to the reference line
				robo.keyPress(KeyEvent.VK_SHIFT);
				for (int i = 0; i < moveUp; i++){
					robo.keyPress(KeyEvent.VK_UP);
					start = System.currentTimeMillis();
					while(System.currentTimeMillis() - start < 50);
					robo.keyRelease(KeyEvent.VK_UP);
				}
				robo.keyRelease(KeyEvent.VK_SHIFT);
				//copy the wikipedia source code
				robo.keyPress(KeyEvent.VK_CONTROL);
				robo.keyPress(KeyEvent.VK_C);
				robo.keyPress(KeyEvent.VK_C);
				robo.keyRelease(KeyEvent.VK_CONTROL);
				//delete the info from the references up
				robo.keyPress(KeyEvent.VK_DELETE);
				robo.keyRelease(KeyEvent.VK_DELETE);
				//move the mouse over to the text area
				robo.mouseMove(1200, 400);
				start = System.currentTimeMillis();
				while (System.currentTimeMillis() - start < 100);
				robo.mousePress(InputEvent.BUTTON1_MASK);
				robo.mouseRelease(InputEvent.BUTTON1_MASK);
				start = System.currentTimeMillis();
				while (System.currentTimeMillis() - start < 1000);
				//paste it into the text areas
				robo.keyPress(KeyEvent.VK_CONTROL);
				robo.keyPress(KeyEvent.VK_V);
				robo.keyRelease(KeyEvent.VK_V);
				robo.keyRelease(KeyEvent.VK_CONTROL);
				PageGenerator page = new PageGenerator();
				start = System.currentTimeMillis();
				while (System.currentTimeMillis() - start < 100);
				
				exceptions = page.createNewPage(city, null, code.getText(), date);
				String ex = "";
				for (int i = 1; i < exceptions.size(); i++){
					ex += exceptions.get(i) + "~";
					if (cleanup){
						JOptionPane.showMessageDialog(null, exceptions.get(i));
					}
				}
				if (!"".equals(ex) && !cleanup){
					File f = BotGui.exceptions;
					FileWriter writer = new FileWriter(f, true);
//					writer.write(Util.convertToEditUrl("/wiki/" + city.getName().replaceAll(" ", "_") + ",_" + BotGui.state)  + "~" + ex + "\n");
					writer.write(url  + "~" + ex + "\n");
					writer.close();
					if (count < BotGui.editToDo){
						count++;
						AutoButton.action();
					}
					return;
				}
				
				String newPage = exceptions.get(0);
				robo.mouseMove(500, 600);
				robo.mousePress(InputEvent.BUTTON1_MASK);
				robo.mouseRelease(InputEvent.BUTTON1_MASK);
				robo.keyPress(KeyEvent.VK_CONTROL);
				robo.keyPress(KeyEvent.VK_HOME);
				robo.keyRelease(KeyEvent.VK_HOME);
				robo.keyPress(KeyEvent.VK_V);
				robo.keyRelease(KeyEvent.VK_V);
				robo.keyRelease(KeyEvent.VK_CONTROL);
				Util.setPasteBuffer(newPage);
				start = System.currentTimeMillis();
				while (System.currentTimeMillis() - start < 700){}
				
				robo.keyPress(KeyEvent.VK_TAB);
				robo.keyRelease(KeyEvent.VK_TAB);
				Util.setPasteBuffer(BotGui.summary);
				
				robo.keyPress(KeyEvent.VK_CONTROL);
				robo.keyPress(KeyEvent.VK_V);
				robo.keyRelease(KeyEvent.VK_V);
				robo.keyRelease(KeyEvent.VK_CONTROL);
				for (int i = 0; i < 3; i++){
					robo.keyPress(KeyEvent.VK_TAB);
					robo.keyRelease(KeyEvent.VK_TAB);
				}
				while (System.currentTimeMillis() - start < 200){}
				if (!cleanup){
					robo.keyPress(KeyEvent.VK_ENTER);
					robo.keyRelease(KeyEvent.VK_ENTER);
				}
				else{
					return;
				}

				boolean done = false;
				while (!done){
					done = true;
					for (int i = 0; i < 10; i++){
						Color test = robo.getPixelColor(791, BotGui.heightCityPage + i);
//						robo.mouseMove(791, 130 + i);
						if (test.getBlue() < 240 || test.getBlue() > 250 || test.getGreen() < 240 || test.getGreen() > 250 ||
								test.getRed() < 240 || test.getRed() > 250){
							done = false;
						}
					}
				}
				start = System.currentTimeMillis();
				while (System.currentTimeMillis() - start < 1000){}
				if (count < BotGui.editToDo && !cleanup){
					AutoButton.action();
					count++;
				}
			} 
			catch (IOException e){
				//connection error
				e.printStackTrace();
			} 
			catch (AWTException e) {
				//robo error
				e.printStackTrace();
			}
		}
	}

}
